import {toRefs} from 'vue'
export default function GlobalMethods(context, $props, {sortables, showData, sIndex, eIndex,checkAll,selectionRow}) {
    let { order, props,count } = toRefs(sortables);
    let tableData = context.attrs.modelValue
    // 排序
    let sortData = () => {
        if (context.attrs["onSortChange"]) {
            context.emit("sort-change", { order, props });
        } else {
            tableData = tableData.sort((a, b) => {
                if (order.value === "asc")
                    return (
                        String(a[props.value]).charCodeAt(0) - String(b[props.value]).charCodeAt(0)
                    );
                else if (order.value === "desc")
                    return (
                        String(b[props.value]).charCodeAt(0) - String(a[props.value]).charCodeAt(0)
                    );
            });
            showData.value = tableData.slice(sIndex.value, eIndex.value);
        }
    }
    // 点击表头事件
    let clickHeaderCell = (row) => {
        if (!row.sortable && !row.dataIndex) return;
        if (props.value) {
          if (props.value === row.sortable || props.value === row.dataIndex) {
            if (count.value === 0) {
              order.value = "asc";
              count.value++;
            } else if (count.value === 1) {
                order.value = "desc";
              count.value++;
            } else if (count.value === 2) {
              order.value = "";
              count.value = 0;
              props.value = "";
            }
          } else {
            // 重置
            order.value = "asc";
            count.value = 1;
          }
        } else {
          if (count.value === 0) {
            order.value = "asc";
            count.value++;
          } else if (count.value === 1) {
            order.value = "desc";
            count.value++;
          } else if (count.value === 2) {
            order.value = "";
            count.value = 0;
            props.value = "";
          }
        }
        props.value = row.sortable || row.dataIndex;
        sortData();
      }
      // 点击选择某项
    let checkRowHandler = (e) => {
        let rowKey = $props.rowKey
        let checkAll = document.getElementById("superTableCheckAll");
        let checkCount = tableData.filter((t) => t.isCheck);
        if(e.isCheck) $props.selectionRowCache.set(e[rowKey],e[rowKey])
        else $props.selectionRowCache.delete(e[rowKey])
        checkAll.value = checkCount.length === tableData.length;
        checkAll.indeterminate =
          checkCount.length > 0 && checkCount.length < tableData.length;
        showData.value = tableData.slice(sIndex.value, eIndex.value);
        context.emit("selection-change", checkCount);
      }

    // 点击全选
    let checkAllHandler = (v) => {
        let rowKey = $props.rowKey
        let checked = v.target.checked;
        let checkAll = document.getElementById("superTableCheckAll");
        selectionRow.value = v ? tableData : [];
        tableData.forEach((r) => {
          r.isCheck = checked
          if(checked) $props.selectionRowCache.set(r[rowKey],r[rowKey])
          else $props.selectionRowCache.delete(r[rowKey])
        });
        checkAll.indeterminate = false;
        showData.value = tableData.slice(sIndex.value, eIndex.value);
        context.emit("selection-change", checked ? tableData : []);
    }
    return {
        sortData,
        clickHeaderCell,
        checkRowHandler,
        checkRowHandler,
        checkAllHandler
    }
}